﻿#include "ssActorThreadPool.h"


namespace StarSeeker
{
	int ssActorThreadPool::s_weight[32] = {
		-1,-1,-1,-1,0,0,0,0,
		1,1,1,1,1,1,1,1,
		2,2,2,2,2,2,2,2,
		3,3,3,3,3,3,3,3
	};

	ssActorThreadPool::ssActorThreadPool()
	{
		m_condGet.BindLock(&m_mutex);
	}

	ssActorThreadPool::~ssActorThreadPool()
	{
		Exit();
	}

	void ssActorThreadPool::Start()
	{
		for (u32 i = 0; i < m_threads.Count(); ++i)
		{
			m_threads[i]->m_ThreadId = i;
			m_threads[i]->m_weight = i < 32 ? ssActorThreadPool::s_weight[i]:0;
			m_threads[i]->StartThread();
		}
	}

	void ssActorThreadPool::Exit()
	{
		for (u32 i = 0; i < m_threads.Count(); ++i)
		{
			m_threads[i]->RequestStop();
		}
		m_condGet.NotifyAll();
		for (u32 i = 0; i < m_threads.Count(); ++i)
		{
			delete m_threads[i];
		}
		m_threads.Clear();
	}

	void ssActorThreadPool::AddThread(ssActorThread* pThread)
	{
		if (m_threads.Count() < 1024)
		{
			m_threads.PushBack(pThread);
		}
	}

	void ssActorThreadPool::AddThreads(ssActorThread* pThreadArr[], u32 count)
	{
		if (m_threads.Count()==0 && count < 1024)
		{
			m_threads.CopyFrom(pThreadArr, count);
		}
	}

	void ssActorThreadPool::NotifyOneWorker()
	{
		m_condGet.NotifyOne();
	}

	void ssActorThreadPool::NotifyAllWorker()
	{
		m_condGet.NotifyAll();
	}

}
